package com.card.framework.util;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.Collection;
import java.util.Date;
import java.util.Map;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRExporterParameter;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperRunManager;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import net.sf.jasperreports.engine.export.JRXlsExporter;
import net.sf.jasperreports.engine.export.JRXlsExporterParameter;
import org.apache.commons.lang.time.DateFormatUtils;


public class ReportUtil {

	public static void exportPdf(String pdfName,InputStream inputStream,HttpServletResponse response,Map parameter,Collection dataSource)
	{
		String headerDisp = "attachment;filename="+pdfName+"_" + DateFormatUtils.format(new Date(),"yyyyMMddHHmmss") + ".pdf";
		try {
			headerDisp = new String(headerDisp.getBytes("gbk"), "iso8859-1");
		} catch (UnsupportedEncodingException e1) {
			e1.printStackTrace();
		}
		response.setHeader("content-disposition", headerDisp);
		response.setContentType("application/octet-stream;charset=UTF-8");  
		try {
			JasperRunManager.runReportToPdfStream(inputStream, response.getOutputStream(),parameter, new JRBeanCollectionDataSource(dataSource));
		} catch (JRException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public static void exportExcel(String pdfName,InputStream inputStream,HttpServletResponse response,Map parameter,Collection dataSource)
	{
		 JasperPrint jasperPrint = null;
		 try {
			jasperPrint = JasperFillManager.fillReport(inputStream,parameter,new JRBeanCollectionDataSource(dataSource));
		 } catch (JRException e) {
			e.printStackTrace();
		 }
	     ByteArrayOutputStream oStream = new ByteArrayOutputStream();
	     JRXlsExporter exporter = new JRXlsExporter(); 
	     exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
	     exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, oStream);
	     exporter.setParameter(JRXlsExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS,Boolean.TRUE);
	     exporter.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET,Boolean.FALSE);
	     exporter.setParameter(JRXlsExporterParameter.IS_WHITE_PAGE_BACKGROUND,Boolean.FALSE);
	     try {
			exporter.exportReport();
		} catch (JRException e) {
			e.printStackTrace();
		}
	    
	    byte[] bytes = oStream.toByteArray();
	   
	    if(bytes != null && bytes.length > 0) {
		      response.reset();
		      response.setContentType("application/vnd.ms-excel");
		      String headerDisp = "attachment;filename="+pdfName+"_" + DateFormatUtils.format(new Date(),"yyyyMMddHHmmss") + ".xls";
				try {
					headerDisp = new String(headerDisp.getBytes("gbk"), "iso8859-1");
				} catch (UnsupportedEncodingException e1) {
					e1.printStackTrace();
				}
			  response.setHeader("content-disposition", headerDisp);
		      response.setContentLength(bytes.length); 
		      ServletOutputStream ouputStream;
		      try {
				ouputStream = response.getOutputStream();
		        ouputStream.write(bytes,0,bytes.length); 
		        ouputStream.flush(); 
		        ouputStream.close();
			  } catch (IOException e) {
				e.printStackTrace();
			  }  
	    }
	}
}
